package cn.com.gaobb.service.impl;

import cn.com.gaobb.dto.ResultDTO;
import cn.com.gaobb.entity.TOrder;
import cn.com.gaobb.entity.TUser;
import cn.com.gaobb.mapper.TOrderMapper;
import cn.com.gaobb.mapper.TUserMapper;
import cn.com.gaobb.service.OrderService;
import cn.com.gaobb.utils.BeanHelper;
import cn.com.gaobb.utils.DataUtil;
import cn.com.gaobb.utils.IdBuilderUtil;
import cn.com.gaobb.vo.*;
import com.auth0.jwt.JWT;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author haopt
 * @version 1.0
 * @ClassName OrderService
 * @Description TODO
 * @date 2021/4/2 10:37
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private TOrderMapper orderMapper;

    @Override
    public ResultDTO<ResIdVO> add(ReqOrderAddVO reqVO, HttpServletRequest request) {
        TOrder tOrder = BeanHelper.copyProperties(reqVO, TOrder.class);
        tOrder.setId(IdBuilderUtil.getId());
        tOrder.setState(1);
        tOrder.setCreateTime(new Date());
        tOrder.setModifyTime(new Date());
        String token = request.getHeader("token");
        String userId = JWT.decode(token).getClaim("userID").asString();
        tOrder.setCreateBy(userId);
        orderMapper.insertSelective(tOrder);
        return ResultDTO.okOf(new ResIdVO(tOrder.getId()));
    }

    @Override
    public ResultDTO update(ReqOrderUpdateVO vo) {
        TOrder tOrder = BeanHelper.copyProperties(vo, TOrder.class);
        tOrder.setModifyTime(new Date());
        orderMapper.updateByPrimaryKeySelective(tOrder);
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO deleteList(ReqOrderDeleteListVO vo) {
        if (CollectionUtils.isEmpty(vo.getIdList())) {
            return ResultDTO.errorOf(1001, "请选择要删除的客户");
        }
        vo.getIdList().forEach(id -> {
            orderMapper.deleteByPrimaryKey(id);
        });
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO<ResOrderDetailVO> detail(String id) {
        TOrder tOrder = orderMapper.selectByPrimaryKey(id);
        ResOrderDetailVO resOrderDetailVO = BeanHelper.copyProperties(tOrder, ResOrderDetailVO.class);
        return ResultDTO.okOf(resOrderDetailVO);
    }

    @Override
    public ResultDTO<ResPageVO<ResOrderDetailVO>> listOfPage(ReqPageVO vo) {
        PageHelper.startPage(vo.getCurrentPage(), vo.getPageSize());
        List<ResOrderDetailVO> list = orderMapper.list(vo.getSearchParem(), vo.getState());
        PageInfo<ResOrderDetailVO> pageInfo = new PageInfo<>(list);
        ResPageVO<ResOrderDetailVO> resPageVO = new ResPageVO<>();
        resPageVO.setRows(pageInfo.getList());
        resPageVO.setCurrentPage(pageInfo.getPageNum());
        resPageVO.setTotal((int) pageInfo.getTotal());
        return ResultDTO.okOf(resPageVO);
    }

    @Override
    public ResultDTO agreeOrDecline(ReqReviewVO vo) {
        if (CollectionUtils.isEmpty(vo.getIdList())) {
            return ResultDTO.errorOf(4001, "请选择数据");
        }
        vo.getIdList().forEach(id -> {
            TOrder order = new TOrder();
            order.setId(id);
            order.setState(vo.getState());
            order.setModifyTime(new Date());
            order.setNote(vo.getNote());
            orderMapper.updateByPrimaryKeySelective(order);
        });
        return ResultDTO.okOf();
    }

    @Override
    public ResultDTO<ResPageVO<ResOrderDetailVO>> listOfPageOfClientId(ReqPageVO vo, HttpServletRequest request) {
        //获取用户信息
        String token = request.getHeader("token");
        String userId = JWT.decode(token).getClaim("userID").asString();
        PageHelper.startPage(vo.getCurrentPage(), vo.getPageSize());
        List<ResOrderDetailVO> list = orderMapper.listOfClientId(vo.getSearchParem(), vo.getState(), userId);
        PageInfo<ResOrderDetailVO> pageInfo = new PageInfo<>(list);
        ResPageVO<ResOrderDetailVO> resPageVO = new ResPageVO<>();
        resPageVO.setRows(pageInfo.getList());
        resPageVO.setCurrentPage(pageInfo.getPageNum());
        resPageVO.setTotal((int) pageInfo.getTotal());
        return ResultDTO.okOf(resPageVO);
    }
}
